<script>

import { useDictDataApi, useDictDataSubmitApi } from '@/api/system/dict'

export default {
  name: 'DictDataEditionVue',
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        dictTypeId: 0,
        dictLabel: '',
        dictValue: '',
        labelClass: '',
        sort: 0,
        remark: ''
      },
      dataRules: {
        dictLabel: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        dictValue: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      this.visible = true
      this.dataForm.id = ''
      const formInstance = this.$refs.dataFormRef
      if (formInstance) {
        formInstance.resetFields()
      }
      if (id) {
        this.getBaseClass(id)
      }
    },
    getBaseClass(id) {
      useDictDataApi(id).then(res => {
        Object.assign(this.dataForm, res.data)
      })
    },
    submitHandle() {
      this.$refs.dataFormRef.validate((valid) => {
        if (!valid) {
          return false
        }
        useDictDataSubmitApi(this.dataForm).then(() => {
          this.$message.success({
            message: '操作成功',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        })
      })
    }
  }
}
</script>

<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增' : '修改'" :append-to-body="true" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px" @keyup.enter="submitHandle()">
      <el-form-item prop="dictValue" label="字典值">
        <el-input v-model="dataForm.dictValue" placeholder="字典值" />
      </el-form-item>
      <el-form-item prop="dictLabel" label="字典标签">
        <el-input v-model="dataForm.dictLabel" placeholder="字典标签" />
      </el-form-item>
      <el-form-item prop="labelClass" label="标签样式">
        <el-select v-model="dataForm.labelClass" style="width: 100%" clearable>
          <el-option
            v-for="item in [
              { value: 'primary', label: 'primary' },
              { value: 'success', label: 'success' },
              { value: 'info', label: 'info' },
              { value: 'warning', label: 'warning' },
              { value: 'danger', label: 'danger' }
            ]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
            <el-tag v-if="item.value === 'primary'" style="width: 100%">{{ item.label }}</el-tag>
            <el-tag v-else style="width: 100%" :type="item.value">{{ item.label }}</el-tag>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="sort" label="排序">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序" />
      </el-form-item>
      <el-form-item prop="remark" label="备注">
        <el-input v-model="dataForm.remark" placeholder="备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
